home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / ST.C < prev    next >
C/C++ Source or Header  |  1988-12-02  |  18KB  |  855 lines

  1. /* OS- and machine-dependent stuff for Atari-ST
  2.  * Adapted from the PC version to compile under Lattice C
  3.  * by Walter Doerr, DG2KK (dg2kk@cup.portal.com)
  4.  *
  5.  * 20-2-88:    added code from the Atari MWC version by Rob Janssen PE1CHL 
  6.  * 13-1-88:    added code needed for the 871225.1 version
  7.  * 24-12-87:    first version, adapted from PC.C from the 870412 release
  8.  */
  9.  
  10. #include "stdio.h"
  11. #include "global.h"
  12. #include "config.h"
  13. #include "mbuf.h"
  14. #include "internet.h"
  15. #include "iface.h"
  16. #include "st.h"
  17. #include "cmdparse.h"
  18.  
  19. /* #include "stdlib.h"    chkml() , malloc , free */
  20.  
  21. #ifdef LATTICE
  22. #include "dos.h"    /* dfind */
  23. #endif
  24. #include "osbind.h"
  25.  
  26. #ifdef    MWC
  27. #include <stat.h>
  28. FILE *stdprt;
  29. extern    char **environ;
  30. #endif
  31.  
  32. #define TRUE -1
  33. #define FALSE 0
  34.  
  35. struct asy asy[ASY_MAX];
  36.  
  37. /* Interface list header */
  38. struct interface *ifaces;
  39.  
  40. unsigned nasy;            /* needed in v871225.1 */
  41.  
  42. char *ttbuf;
  43. char *rsbuf;            /* location of memory alloc'd for rs232 buf */
  44.  
  45. #ifdef SCREEN
  46. char *newscreen, *newscradr;    /* 32k memory allocated to 2nd video screen */
  47. long logscreen, physcreen;
  48. int toggle = 0;            /* toggle between normal and trace screen */
  49. int printtrace = 0;        /* send trace to printer (controlled by F2) */
  50. #endif
  51.  
  52. char *shell,*getenv();        /* Name of command shell for exec */
  53. int    rows=25;        /* Number of text rows on screen (may be 50) */
  54.  
  55. /* called during init b4 anything is printed on the screen.
  56.  * PC.C does a lot of memory allocation stuff here.
  57.  * We just set the cursor to "blink" and allocate memory for a second video
  58.  * screen.
  59.  */
  60. ioinit()
  61. {
  62.     char *lmalloc();    /* Takes a long arg */
  63.     unsigned long ptr;
  64.  
  65. #ifdef SCREEN
  66.     newscradr = lmalloc(32*1024L);    /* allocate 32k for 2nd video screen */
  67.     ptr = (unsigned long)newscradr;
  68.     newscreen = (char *)((ptr+255) & 0xffffff00L);    /* 256 byte boundary */
  69.  
  70.     physcreen = (long)Physbase();    /* remember displayed screen address */
  71.     logscreen = (long)Logbase();    /* remember output screen address */
  72.     Cconws("\033j");        /* Save cursor position */
  73.     (void) Setscreen(newscreen,-1L,-1);    /* switch to 2nd screen */
  74.     (void) Vsync();            /* wait for vsync */
  75.     Cconws("\033H\033J");        /* cursor home, clear screen */
  76.     Cconws("\033k");        /* restore cursor position */
  77.     (void) Setscreen(logscreen,-1L,-1);    /* restore old screen */
  78.     (void) Vsync();            /* wait for vsync */
  79. #endif
  80.     printf("\033v\033e\n");        /* Autowrap on, Cursor on */
  81.     (void) Cursconf(2,0);        /* make the cursor blink (3=steady) */
  82. #ifdef MWC
  83.     stdprt = fopen("prn:","w");
  84. #endif
  85.  
  86.     shell = getenv("NROWS");
  87.     if (shell != NULL)
  88.         rows=atoi(shell);
  89.  
  90.     shell = getenv("SHELL");
  91.     if (shell == NULL)
  92.         shell="\\bin\\gulam.prg";
  93. }
  94.  
  95. /* Called just before exiting. 
  96.  * delete temp files.
  97.  * free memory allocated to the rs-232 and midi buffers.
  98.  */
  99. iostop()
  100. {
  101.     /* free memory allocated to 2nd video screen */
  102.  
  103. #ifdef SCREEN
  104.     dispscreen(0);        /* switch back to original screen */
  105.     free(newscradr);
  106. #endif
  107.  
  108.     /* delete all temp files that have accumulated */
  109.     (void) tmpdel();
  110.  
  111.     /* free memory allocated to RS-232/MIDI I/O buffers */
  112.     while (ifaces != NULLIF) {
  113.         if (ifaces->stop != NULLFP)
  114.             (*ifaces->stop)(ifaces);
  115.         ifaces = ifaces->next;
  116.     }
  117.  
  118.     printf("\n");
  119. }
  120.  
  121.  
  122. /* checks the time then ticks and updates ISS */
  123. static int32 clkval = 0;
  124. void
  125. check_time()
  126. {
  127.     int32 iss();            /* initial sequence number */
  128.     int32 clksec();
  129.     if(clkval != clksec()){
  130.          clkval = clksec();
  131.         icmpclk();
  132.         tick();
  133.         (void)iss();
  134.     }
  135. }
  136.  
  137. /* returns the number of seconds from system clock */
  138. static int32
  139. clksec()
  140. {
  141.     long tloc;
  142.     time(&tloc);
  143.     return (tloc);
  144. }
  145.  
  146.  
  147. /* Initialize async port "dev" (adapted from PE1CHL) */
  148. int
  149. asy_init(dev,bufsize)
  150. int16 dev;
  151. unsigned bufsize;
  152. {
  153.     register struct iorec *ip;
  154.     register struct asy *ap;
  155.     char *bufp;
  156. /*    char i_state; */
  157.  
  158. #ifdef DEBUG
  159.     printf("asy_init: dev=%d bufsize=%d\n",dev,bufsize);
  160.     fflush(stdout);
  161. #endif
  162.     ap = &asy[dev];
  163.  
  164.     if (ap->addr != RS232 && ap->addr != MIDI) {
  165.         printf("asy_init(%d): unknown interface\n",dev);
  166.         return -1;
  167.     }
  168.  
  169.     /* force user to allocate more memory than he already has.
  170.      * If no memory is allocated, asy_stop() may behave funny...
  171.      */
  172.     if (bufsize <= 256)    /* only allocate a bigger buffer */
  173.         return -1;
  174.  
  175.     if ((bufp = malloc(bufsize)) == NULLCHAR){
  176.         printf("asy_init(%d): no memory for rx buffer\n",dev);
  177.         return -1;
  178.     }
  179.  
  180.     /* Save original IOREC values */
  181.  
  182.     ip = Iorec((ap->addr)-1);    /* Iorec wants AUX: = 0, MIDI = 2 */
  183.  
  184.     Jdisint(12);            /* disable RS-232 interrupt */
  185.  
  186.     ap->in = ip;
  187.     memcpy(&ap->oldin,ip);
  188.     if (ap->addr == RS232) {    /* increase RS-232 transmit buffer? */
  189.         ip++;
  190.         ap->out = ip;
  191.         memcpy(&ap->oldout,ip);
  192.     }
  193.  
  194.     /* Set up receiver FIFO */
  195.     ap->in->ibuf = bufp;
  196.     ap->in->ibufsiz = bufsize;
  197.     ap->in->ibufhd = ap->in->ibuftl = 0;
  198.     ap->in->ibuflow = 0;
  199.     ap->in->ibufhi = bufsize;
  200.  
  201.     if (ap->addr == RS232) {
  202.         /* clear transmitter FIFO */
  203.         ap->out->ibufhd = ap->out->ibuftl = 0;
  204.         ap->out->ibuflow = 0;
  205.         ap->out->ibufhi = ap->out->ibufsiz;
  206.     }
  207.  
  208.     Jenabint(12);            /* enable RS-232 interrupts */
  209.  
  210.     if (ap->addr == RS232)
  211.         Rsconf(-1,0,-1,0,0,0);    /* 8 bits, no parity */
  212.  
  213. #ifdef DEBUG
  214.     printf("asy_init: Iorecs in: 0x%lx out: 0x%lx\n",ap->in,ap->out);
  215.     printf("    inbuf: 0x%lx outbuf: 0x%lx\n",ap->in->ibuf,
  216.         ap->out->ibuf);
  217. #endif
  218. }
  219.  
  220.  
  221. /* asy_stop restores old iorec and frees memory allocated to the RS-232/MIDI
  222.  * buffers. (from PE1CHL)
  223.  */
  224. int
  225. asy_stop(iface)
  226. struct interface *iface;
  227. {
  228.     register struct asy *ap;
  229. /*    char i_state; */
  230.  
  231. #ifdef DEBUG
  232.     printf("asy_stop: iface=0x%lx dev=%d\n",iface,iface->dev);
  233.     fflush(stdout);
  234. #endif
  235.     ap = &asy[iface->dev];
  236.  
  237.     (void) Jdisint(12);        /* disable RS-232 interrupts */
  238.  
  239.     free(ap->in->ibuf);        /* free the buffer */
  240.  
  241.     /* Restore old iorecs */
  242.     memcpy(ap->in,&ap->oldin,sizeof(struct iorec));
  243.     if (ap->addr == RS232)
  244.         memcpy(ap->out,&ap->oldout,sizeof(struct iorec));
  245.  
  246.     (void) Jenabint(12);        /* enable RS-232 interrupts */ 
  247. }
  248.  
  249.  
  250. /* Set async line speed */
  251. int
  252. asy_speed(dev,speed)
  253. int dev;
  254. int speed;
  255. {
  256.     int baud; /* int result; */
  257.     register int sp;
  258.     long sav_ssp;
  259.  
  260.     if (speed <= 0 || dev >= nasy)
  261.         return -1;
  262.  
  263.     asy[dev].speed = speed;        /* shouldn't this be done in slip.c? */
  264.  
  265.     switch (asy[dev].addr) {
  266.  
  267.     case RS232:
  268.         switch (speed) {
  269.         case 300:
  270.             baud = 9;    /* how slow can you get? :-) */
  271.             break;
  272.         case 1200:
  273.             baud = 7;
  274.             break;
  275.         case 2400:
  276.             baud = 4;
  277.             break;
  278.         case 4800:
  279.             baud = 2;
  280.             break;
  281.         case 9600:
  282.             baud = 1;
  283.             break;
  284.         case 19200:
  285.             baud = 0;
  286.             break;
  287.         default:
  288.             printf("asy_speed: unknown RS-232 speed (%d).\n",speed);
  289.             return -1;
  290.         }
  291.         (void) Rsconf(baud,0,0x88,-1,-1,-1);    /* no flow control */
  292.         break;
  293.  
  294.     case MIDI:
  295.         /* midi can run on 500000 or 614400Hz clock (hardware mod)
  296.          */
  297.         switch (speed)
  298.         {
  299.         case 0x9600:        /* = 38400 unsigned... (hardware mod) */
  300.         case 31250:        /* normal MIDI baudrate */
  301.             sp = 0x95;    /* /16 */
  302.             break;
  303.  
  304.         case 9600:        /* 9600 Baud requires hardware mod */
  305.         case 7812:        /* 7812 Baud on an unmodified Atari */
  306.             sp = 0x96;    /* /64 */
  307.             break;
  308.  
  309.         default:
  310.             printf("asy_speed: unknown MIDI speed (%d).\n",speed);
  311.             return -1;
  312.         }
  313.  
  314.         sav_ssp = Super(NULL);        /* switch to Supervisor mode */
  315.         *((char *) 0xfffffc04L) = 0x03; /* Reset 6850 */
  316.         hiword(0);            /* spend some time */
  317.         *((char *) 0xfffffc04L) = sp;    /* set new divider ratio */
  318.         Super(sav_ssp);            /* back to User mode */
  319.  
  320.         break;
  321.     }
  322.     asy_flush(dev);
  323. }
  324.  
  325.  
  326. /* flush the input buffer of device dev (either aux: or midi).
  327.  * May be useful, because setting the baudrate causes an 0x7f to be sent.
  328.  */
  329. asy_flush(dev)
  330. int dev;
  331. {
  332.     int st_dev;
  333.     long c;                    /* Bconin returns a long */
  334.     st_dev = asy[dev].addr;
  335.  
  336.     while (Bconstat(st_dev) == -1) {    /* at least 1 char avlb */
  337.         c = Bconin(st_dev);
  338.     }
  339. }
  340.  
  341.  
  342. /* Send a buffer to serial transmitter */
  343. asy_output(dev,buf,cnt)
  344. unsigned dev;
  345. char *buf;
  346. unsigned short cnt;
  347. {
  348.     int st_dev; /* int c; */
  349.     unsigned short i = 0;
  350.  
  351.     if (dev >= nasy)
  352.         return -1;
  353.  
  354.     st_dev = asy[dev].addr;
  355.     for (i = 0